{% extends "safe/safe_base.html" %}
{% block title %}操作日志{% endblock %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-table.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/select2.min.css') }}">

{% endblock %}

{% block page_content %}
<h1 class="page-header">操作日志</h1>
{{ super() }}

<div class="col-md-12">
    <div class="panel-body" style="padding-bottom:0px;">
        <ul id="myTab" class="nav nav-tabs">
            <li class="active">
                <a href="#staff" data-toggle="tab" onclick="setTable('staff')">内部账户</a>
            </li>
            <li>
                <a href="#user" data-toggle="tab" onclick="setTable('user')">外部账户</a>
            </li>
        </ul>
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade in active" id="staff">
                <table id="tb_staff"></table>
            </div>
            <div class="tab-pane fade" id="user">
                <table id="tb_user"></table>
            </div>
        </div>
    </div>
</div>
<div>
    <input type="hidden" id="xfrs" name="xfrs" value="{{ xfrs }}">
</div>


{% endblock %}

{% block scripts %}
<!-- <script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script> -->
{{ super() }}
<script src="{{ url_for('static', filename='js/layer.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap-table.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap-table-zh-CN.min.js') }}"></script>


<script>
$(function () {
    //初始化Table
    var table_staff = new TableInit("tb_staff");
    table_staff.Init();
    var table_user = new TableInit("tb_user");
    table_user.Init();
});


var TableInit = function (table_id) {
    var oTableInit = new Object();
    var urls = ['/safe/record/get_list?list_type=staff', '/safe/record/get_list?list_type=user']
    var ids = ["tb_staff", "tb_user"];
    var index = ids.indexOf(table_id);
    var url = urls[index];
    var columns;
    if(table_id==='tb_staff') {
        columns = [{
                field: 'id',
                title: '#',
                align: 'center',
                visible: false,
                },
                {
                field: 'ip',
                title: 'IP',
                align: 'center',
                },
                {
                    field: 'staff_number',
                    title: '工码',
                    align: 'center',
                    }, {
                        field: 'name',
                        title: '操作人',
                align: 'center',
                    }, {
                        field: 'operation',
                        title: '操作内容',
                align: 'center',
                    }, {
                field: 'operation_time',
                title: '操作时间',
                align: 'center',
                }, {
                        field: 'operate',
                        title: '操作',
                        align: 'center',
                        // events: operateEvents,
                        formatter: operateFormatter_staff
                    },]
    } else if (table_id==='tb_user') {
        columns = [
            {
                field: 'id',
                title: '#',
                align: 'center',
                visible: false,
            },
            {
                field: 'ip',
                title: 'IP',
                align: 'center',
            },
            {
                field: 'name',
                title: '操作人',
                align: 'center',
            },
            {
                field: 'phone',
                title: '号码',
                align: 'center',
            },
            {
                field: 'operation',
                title: '操作内容',
                align: 'center',
            },
            {
                field: 'operation_time',
                title: '操作时间',
                align: 'center',
            },
            {
                field: 'area',
                title: '地区',
                align: 'center',
            },
            {
                field: 'operate',
                title: '操作',
                align: 'center',
                // events: operateEvents,
                formatter: operateFormatter_staff
            },
        ]
    }
    //初始化Table
    table = document.getElementById(table_id);
    oTableInit.Init = function () {
        $(table).bootstrapTable({
            url: url,  //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: oTableInit.queryParams,//传递参数（*）
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 50,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            columns: columns
            });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = { 
                limit: params.limit,   //页面大小
                offset: params.offset,  //页码
                number: $("#number").val(),
                start: $("#start").val(),
                end: $("#end").val(),
                name: $("#name").val(),
                operation: $("#operation").val(),
                search_type: $("#search_type").val(),
                tb_name: $("#tb_name").val(),
            };
        return temp;
    };
    return oTableInit;
};
var selected_table = 'staff';

var setTable = function(table){
    selected_table = table;
}

var search = function(){
    document.getElementById('tb_name').value = selected_table;
    $('#tb_' + selected_table).bootstrapTable('refresh', {'silent': true});
}

var is_self = function (value, row, index) {

}



function operateFormatter_staff(value, row, index) {
        var info = row.info;
        var status = info.status;
        if (status === 0){
            return [
                "<a href='javascript:void(0)' onclick='freezeLayer(" + JSON.stringify(row.staff_number) + ","+ row.id + "," + JSON.stringify(row.info) + ")'>",
                '冻结',
                '</a>',
            ].join('');

        };
        if (status === 1) {
            return [
                '<a href="javascript:void(0)" onclick="unfreezeLayer(' + row.id + ')" >',
                '解冻',
                '</a>',
            ].join('');
        }
};


    var freezeLayer = function (staff_number, id, info) {
        var now_number = info.staff_number // 当前登录账户的ID
        var role = info.role;   // admin
        // console.log(staff_number);  // 要冻结的账户ID
        // console.log(now_number);
        // console.log(role);// 要冻结的账户role
        if (staff_number === now_number) {
            layer.alert('你不能冻结自己!')
            return false;
        }
        if (role === 'super') {
            layer.alert('该账户不能被冻结!')
            return false;
        }

        var url = '/safe/freeze' + '/' + id;
        method = 'post'
        layer.prompt({title: '填写冻结原因 当前冻结用户：' + staff_number, formType: 2, }, function(text, index){
            var form = {};
            form['reason'] = text;
            form['id'] = id;
            form['xfrs'] = $("#xfrs").val();
            console.log('freeze form', form);
            var success = function (r) {
                if (r.success) {
                    layer.close(index);
                    layer.msg('冻结成功！');
                    location.reload();
                } else {
                    layer.alert(r.message);
                }
            };
            ajaxPost(url, method, form, success, error);
        });
        }

    var unfreezeLayer = function (id) {
        var url = '/safe/unfreeze' + '/' + id;
        var method = 'post';
        var form = {};
        form['id'] = id;
        form['xfrs'] = $("#xfrs").val();
        layer.confirm('确定解冻账户？', {
              btn: ['确定','取消'] //按钮
            }, function(){
                console.log('unfreeze form', form);
                var success = function (r) {
                    if (r.success) {
                        layer.msg('解冻成功', {icon: 1});
                        location.reload();
                    } else {
                        layer.alert(r.message);
                    }
                };
                ajaxPost(url, method, form, success, error);
            }, function(){
              
            });
    };

    
    var ajaxPost = function (url, method, form, success, error) {
        var request = {
            url: url,
            type: method,
            contentType: 'application/json',
            success: function (r) {
                success(r);
            },
            error: function (err) {
                r = {
                    success: false,
                    data: err
                };
                error(r);
            }
        };
        if (method === 'post') {
            var data = JSON.stringify(form);
            request.data = data;
        }
        $.ajax(request);
    };
    var success = function (r) {
        console.log(r.message);
    };
    var error = function (err) {
        
    };   
    var bindActions = function () {

    }
    $(document).ready(function () {
        bindActions();
    });



</script>
<script>
    nav = document.getElementById("nav_operation_log");
    nav.setAttribute("class", "active");
</script>
{% endblock %}
